package com.jmuok.web.fff.util;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;

@Configurable
public class ApplicationHelper {

	@Autowired
	public VaadinApplicationObjectSupport support;

	private static ApplicationHelper self;

	private ApplicationHelper() {
	}

	public static synchronized ApplicationHelper instance() {
		if (self == null) {
			self = new ApplicationHelper();
		}
		return self;
	}

	public static ApplicationContext getApplicationContext(Application app) {
		ServletContext sc = ((WebApplicationContext) app.getContext())
				.getHttpSession().getServletContext();
		return WebApplicationContextUtils.getWebApplicationContext(sc);
	}

	public String getMessage(String key) {
		return support.getMessage(key);
	}

	public String getMessage(String key, String... args) {
		return support.getMessage(key, args);
	}

	// public String getMessage(String code, Object[] args, Locale locale)
	// throws NoSuchMessageException {
	//
	// return ms.getMessage(code, args, locale);
	// }
	//
	// public String getMessage(String code, Object[] args, String
	// defaultMessage,
	// Locale locale) {
	// return ms.getMessage(code, args, defaultMessage, locale);
	// }
}
